home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 October / DPPCPRO1005.ISO / Download / Web Developer / webdeveloper.xpi / chrome / webdeveloper.jar / content / webdeveloper / tools.js < prev    next >
Encoding:
JavaScript  |  2005-03-21  |  12.3 KB  |  309 lines

  1. // Displays the tools menu
  2. function webdeveloper_displayToolsMenu(menu, separatorName, tooltips)
  3. {
  4.     const preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  5.     const validatorSeparator = menu.getElementsByAttribute("id", separatorName)[0];
  6.  
  7.     var description           = null;
  8.     var descriptionPreference = null;
  9.     var key                   = null;
  10.     var menuItem              = document.createElement("menuitem");
  11.     var url                   = null;
  12.     var toolsCount            = 0;
  13.  
  14.     // If the tools count preference is set
  15.     if(preferencesService.prefHasUserValue("webdeveloper.tool.count"))
  16.     {
  17.         toolsCount = preferencesService.getIntPref("webdeveloper.tool.count");
  18.     }
  19.  
  20.     webdeveloper_removeGeneratedMenuItems(menu);
  21.  
  22.     // Loop through the possible tools
  23.     for(var i = 1; i <= toolsCount; i++)
  24.     {
  25.         description = "webdeveloper.tool." + i + ".description";
  26.         key         = "webdeveloper.tool." + i + ".key";
  27.         url         = "webdeveloper.tool." + i + ".url";
  28.  
  29.         // If the description and URL are set
  30.         if(preferencesService.prefHasUserValue(description) && preferencesService.prefHasUserValue(url))
  31.         {
  32.             descriptionPreference = preferencesService.getComplexValue(description, Components.interfaces.nsISupportsString).data.trim();
  33.  
  34.             // If the description is not blank
  35.             if(descriptionPreference != "")
  36.             {
  37.                 menuItem = document.createElement("menuitem");
  38.                 menuItem.setAttribute("class", "webdeveloper-generated-menu");
  39.                 menuItem.setAttribute("label", descriptionPreference);
  40.                 menuItem.setAttribute("oncommand", "webdeveloper_loadURL('" + preferencesService.getComplexValue(url, Components.interfaces.nsISupportsString).data.trim() + escape(getBrowser().currentURI.spec) + "')");
  41.  
  42.                 // If the key preference is set
  43.                 if(preferencesService.prefHasUserValue(key))
  44.                 {
  45.                     menuItem.setAttribute("key", key);
  46.                 }
  47.  
  48.                 // If displaying tooltips
  49.                 if(tooltips)
  50.                 {
  51.                     menuItem.setAttribute("tooltiptext", descriptionPreference);
  52.                 }
  53.  
  54.                 menu.insertBefore(menuItem, validatorSeparator);
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60. // Validates local CSS
  61. function webdeveloper_validateLocalCSS()
  62. {
  63.     const mainTabBox         = getBrowser().mTabBox;
  64.     const documentList       = webdeveloper_getDocuments(getBrowser().browsers[mainTabBox.selectedIndex].contentWindow, new Array());
  65.     const oldTab             = getBrowser().selectedTab;
  66.     const oldURL             = getBrowser().currentURI.spec;
  67.     const preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  68.     const request            = new XMLHttpRequest();
  69.  
  70.     var cssRule        = null;
  71.     var formElement    = null;
  72.     var generatedPage  = null;
  73.     var inputElement   = null;
  74.     var pageDocument   = null;
  75.     var styleSheet     = null;
  76.     var styleSheetHref = null;
  77.     var styleSheetList = null;
  78.     var styleText      = "";
  79.  
  80.     // Loop through the documents
  81.     for(var i = 0; i < documentList.length; i++)
  82.     {
  83.         pageDocument   = documentList[i];
  84.         styleSheetList = pageDocument.styleSheets;
  85.  
  86.         // Loop through the style sheets
  87.         for(var j = 0; j < styleSheetList.length; j++)
  88.         {
  89.             styleText += webdeveloper_retrieveStyleSheetText(pageDocument, styleSheetList[j]);
  90.         }
  91.     }
  92.  
  93.     generatedPage = webdeveloper_generatePage("");
  94.  
  95.     // This must be done to make generated content render
  96.     request.open("get", "about:blank", false);
  97.     request.send("");
  98.  
  99.     formElement = generatedPage.content.document.createElement("form")
  100.     formElement.setAttribute("method", "get");
  101.     formElement.setAttribute("action", "http://jigsaw.w3.org/css-validator/validator");
  102.  
  103.     inputElement = generatedPage.content.document.createElement("input");
  104.     inputElement.setAttribute("type", "hidden");
  105.     inputElement.setAttribute("name", "profile");
  106.     inputElement.setAttribute("value", "css2");
  107.     formElement.appendChild(inputElement);
  108.  
  109.     inputElement = generatedPage.content.document.createElement("input");
  110.     inputElement.setAttribute("type", "hidden");
  111.     inputElement.setAttribute("name", "usermedium");
  112.     inputElement.setAttribute("value", "all");
  113.     formElement.appendChild(inputElement);
  114.  
  115.     inputElement = generatedPage.content.document.createElement("input");
  116.     inputElement.setAttribute("type", "hidden");
  117.     inputElement.setAttribute("name", "warning");
  118.     inputElement.setAttribute("value", "2");
  119.     formElement.appendChild(inputElement);
  120.  
  121.     inputElement = generatedPage.content.document.createElement("input")
  122.     inputElement.setAttribute("type", "hidden");
  123.     inputElement.setAttribute("name", "text");
  124.     inputElement.setAttribute("value", styleText);
  125.     formElement.appendChild(inputElement);
  126.  
  127.     generatedPage.content.document.body.appendChild(formElement);
  128.     formElement.submit();
  129.  
  130.     // If the open tabs in background preference is set to true
  131.     if(preferencesService.prefHasUserValue("webdeveloper.open.tabs.background") && preferencesService.getBoolPref("webdeveloper.open.tabs.background"))
  132.     {
  133.         getBrowser().selectedTab = oldTab;
  134.     }
  135. }
  136.  
  137. // Validates a local HTML file
  138. function webdeveloper_validateLocalHTML()
  139. {
  140.     const oldTab             = getBrowser().selectedTab;
  141.     const oldURL             = getBrowser().currentURI.spec;
  142.     const preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  143.     const request            = new XMLHttpRequest();
  144.  
  145.     var defaultCharacterSet = null;
  146.     var formElement         = null;
  147.     var generatedPage       = null;
  148.     var inputElement        = null;
  149.  
  150.     // If the character set preference is set
  151.     if(preferencesService.prefHasUserValue("intl.charset.default"))
  152.     {
  153.         defaultCharacterSet = preferencesService.getCharPref("intl.charset.default");
  154.     }
  155.  
  156.     preferencesService.setCharPref("intl.charset.default", getBrowser().contentDocument.characterSet);
  157.  
  158.     generatedPage = webdeveloper_generatePage("");
  159.  
  160.     // This must be done to make generated content render
  161.     request.open("get", oldURL, false);
  162.     request.send("");
  163.  
  164.     formElement = generatedPage.content.document.createElement("form")
  165.     formElement.setAttribute("method", "post");
  166.     formElement.setAttribute("action", "http://validator.w3.org/check");
  167.  
  168.     inputElement = generatedPage.content.document.createElement("input");
  169.     inputElement.setAttribute("type", "hidden");
  170.     inputElement.setAttribute("name", "verbose");
  171.     inputElement.setAttribute("value", "1");
  172.     formElement.appendChild(inputElement);
  173.  
  174.     inputElement = generatedPage.content.document.createElement("input");
  175.     inputElement.setAttribute("type", "hidden");
  176.     inputElement.setAttribute("name", "fragment");
  177.     inputElement.setAttribute("value", request.responseText);
  178.     formElement.appendChild(inputElement);
  179.  
  180.     generatedPage.content.document.body.appendChild(formElement);
  181.     formElement.submit();
  182.  
  183.     // If the default character set is set
  184.     if(defaultCharacterSet)
  185.     {
  186.         preferencesService.setCharPref("intl.charset.default", defaultCharacterSet);
  187.     }
  188.     else
  189.     {
  190.         preferencesService.clearUserPref("intl.charset.default");
  191.     }
  192.  
  193.     // If the open tabs in background preference is set to true
  194.     if(preferencesService.prefHasUserValue("webdeveloper.open.tabs.background") && preferencesService.getBoolPref("webdeveloper.open.tabs.background"))
  195.     {
  196.         getBrowser().selectedTab = oldTab;
  197.     }
  198. }
  199.  
  200. // Tidies the HTML before validating
  201. function webdeveloper_validateTidiedHTML()
  202. {
  203.     const oldTab             = getBrowser().selectedTab;
  204.     const oldURL             = getBrowser().currentURI.spec;
  205.     const preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  206.     const request            = new XMLHttpRequest();
  207.  
  208.     var formElement   = null;
  209.     var generatedPage = null;
  210.     var inputElement  = null;
  211.     var output        = "";
  212.  
  213.     generatedPage = webdeveloper_generatePage("");
  214.  
  215.     // This must be done to make generated content render
  216.     request.open("get", oldURL, false);
  217.     request.send("");
  218.  
  219.     output = request.responseText;
  220.  
  221.     // If closing <br> tags
  222.     if(preferencesService.prefHasUserValue("webdeveloper.tidied.html.close.br") && preferencesService.getBoolPref("webdeveloper.tidied.html.close.br"))
  223.     {
  224.         output = output.replace(new RegExp("<br>", "gi"), "<br />");
  225.     }
  226.  
  227.     // If escaping ampersands
  228.     if(preferencesService.prefHasUserValue("webdeveloper.tidied.html.escape.ampersand") && preferencesService.getBoolPref("webdeveloper.tidied.html.escape.ampersand"))
  229.     {
  230.         output = output.replace(new RegExp("&(?!#?[xX]?(?:[0-9a-fA-F]+|\w{1,8});)", "gi"), "&");
  231.     }
  232.  
  233.     formElement = generatedPage.content.document.createElement("form")
  234.     formElement.setAttribute("method", "post");
  235.     formElement.setAttribute("action", "http://validator.w3.org/check");
  236.  
  237.     inputElement = generatedPage.content.document.createElement("input");
  238.     inputElement.setAttribute("type", "hidden");
  239.     inputElement.setAttribute("name", "verbose");
  240.     inputElement.setAttribute("value", "1");
  241.     formElement.appendChild(inputElement);
  242.  
  243.     inputElement = generatedPage.content.document.createElement("input");
  244.     inputElement.setAttribute("type", "hidden");
  245.     inputElement.setAttribute("name", "doctype");
  246.     inputElement.setAttribute("value", "HTML 4.01 Transitional");
  247.     formElement.appendChild(inputElement);
  248.  
  249.     inputElement = generatedPage.content.document.createElement("input");
  250.     inputElement.setAttribute("type", "hidden");
  251.     inputElement.setAttribute("name", "fbd");
  252.     inputElement.setAttribute("value", "1");
  253.     formElement.appendChild(inputElement);
  254.  
  255.     inputElement = generatedPage.content.document.createElement("input");
  256.     inputElement.setAttribute("type", "hidden");
  257.     inputElement.setAttribute("name", "ss");
  258.     inputElement.setAttribute("value", "1");
  259.     formElement.appendChild(inputElement);
  260.  
  261.     inputElement = generatedPage.content.document.createElement("input");
  262.     inputElement.setAttribute("type", "hidden");
  263.     inputElement.setAttribute("name", "fragment");
  264.     inputElement.setAttribute("value", output);
  265.     formElement.appendChild(inputElement);
  266.  
  267.     generatedPage.content.document.body.appendChild(formElement);
  268.     formElement.submit();
  269.  
  270.     // If the open tabs in background preference is set to true
  271.     if(preferencesService.prefHasUserValue("webdeveloper.open.tabs.background") && preferencesService.getBoolPref("webdeveloper.open.tabs.background"))
  272.     {
  273.         getBrowser().selectedTab = oldTab;
  274.     }
  275. }
  276.  
  277. // Validates the page against the validator
  278. function webdeveloper_validateURL(validator)
  279. {
  280.     const preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  281.  
  282.     var validatorURL = "";
  283.  
  284.     // If the validator preference is set
  285.     if(preferencesService.prefHasUserValue(validator) && preferencesService.getComplexValue(validator, Components.interfaces.nsISupportsString).data.trim() != "")
  286.     {
  287.         validatorURL = preferencesService.getComplexValue(validator, Components.interfaces.nsISupportsString).data.trim();
  288.     }
  289.     else
  290.     {
  291.         const stringBundle = document.getElementById("webdeveloper-string-bundle");
  292.  
  293.         alert(stringBundle.getString("webdeveloper_validateURLConfigure"));
  294.         webdeveloper_options();
  295.  
  296.         // If the validator preference is set
  297.         if(preferencesService.prefHasUserValue(validator))
  298.         {
  299.             validatorURL = preferencesService.getComplexValue(validator, Components.interfaces.nsISupportsString).data.trim();
  300.         }
  301.     }
  302.  
  303.     // If the validator URL is not empty
  304.     if(validatorURL != "")
  305.     {
  306.         webdeveloper_loadURL(validatorURL + escape(getBrowser().currentURI.spec));
  307.     }
  308. }
  309.